/**
 * 项目名：student
 * 修改历史：
 * 作者： MZ
 * 创建时间： 2016年1月6日-上午9:59:58
 */
package studentsinfomanagement.dao;

import studentsinfomanagement.bean.Teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 模块说明： 老师增删改查
 * 
 */
public class TeacherDAO extends BaseDAO {

	private static TeacherDAO td = null;

	/**
	 * 采用单例模式
	 * @return 该类的实例
	 */
	public static synchronized TeacherDAO getInstance() {
		if (td == null) {
			td = new TeacherDAO();
		}
		return td;
	}

	/**
	 * 老师登录系统
	 * @param tno 学工号
	 * @param password 密码
	 * @return
	 */
	public boolean queryForLogin(String tno, String password) {
		boolean result = false;
		if (tno.length() == 0 || password.length() == 0) {
			return result;
		}
		//编写sql语句，准备参数
		String sql = "select * from teacher where tno=? and password=?";
		String[] param = { tno, password };
		//调用数据库工具的查询方法
		rs = db.executeQuery(sql, param);
		//查询得到结果则验证通过
		try {
			if (rs.next()) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			destroy();
		}
		return result;
	}

	/**
	 * 	添加老师信息
	 * @param teacher 老师对象
	 * @return 操作结果
	 */
	public boolean add(Teacher teacher) {
		boolean result = false;
		if (teacher== null) {
			return result;
		}
		try {
			// 检查老师是否存在
			if (queryByTno(teacher.getTno()) == 1) {
				return result;
			}
			// 执行插入语句
			String sql = "insert into teacher(name,tno,sex,dno,password) values(?,?,?,?,?)";
			String[] param = { teacher.getName(), teacher.getTno(), teacher.getSex(),
					teacher.getDepartment(), teacher.getPassword() };
			if (db.executeUpdate(sql, param) == 1) {
				result = true;
			}
		} catch (SQLException se) {
			result = false;
		} finally {
			destroy();
		}
		return result;
	}

	/**
	 * 删除老师信息
	 * @param teacher 老师对象
	 * @return 操作结果
	 */
	public boolean delete(Teacher teacher) {
		boolean result = false;
		if (teacher == null) {
			return result;
		}
		String sql = "delete from teacher where tno=?";
		String[] param = { teacher.getTno() };
		int rowCount = db.executeUpdate(sql, param);
		if (rowCount == 1) {
			result = true;
		}
		destroy();
		return result;
	}

	/**
	 * 更新数据库中teacher对象信息
	 * @param teacher 老师对象
	 * @return
	 */
	public boolean update(Teacher teacher) {
		boolean result = false;
		if (teacher == null) {
			return result;
		}
		try {
			// 查询学生是否存在
			if (queryByTno(teacher.getTno()) == 0) {
				return result;
			}
			// 执行更新操作
			String sql = "update teacher set sex=?,password=? where name=? and tno=?";
			String[] param = { teacher.getSex(), teacher.getPassword(), teacher.getName(), teacher.getTno() };
			int rowCount = db.executeUpdate(sql, param);
			if (rowCount == 1) {
				result = true;
			}
		} catch (SQLException se) {
			se.printStackTrace();
		} finally {
			destroy();
		}
		return result;
	}

	/**
	 * 根据学号查询学生是否存在
	 * @param tno 学号
	 * @return 查询结果 1-学生存在 0-学生不存在
	 * @throws SQLException
	 */
	private int queryByTno(String tno) throws SQLException {
		int result = 0;
		if ("".equals(tno) || tno == null) {
			return result;
		}
		//准备sql查询语句
		String checkSql = "select * from teacher where tno=?";
		//准备参数
		String[] checkParam = { tno };
		//调用数据库工具类的查询方法，执行查询语句
		rs = db.executeQuery(checkSql, checkParam);
		//如果查询结果集合不为空表示查询到了学生信息
		if (rs.next()) {
			result = 1;
		}
		return result;
	}

	/**
	 * 通过学工号获取老师信息
	 * @param tno 学工号
	 * @return 老师对象
	 */
	public Teacher getTeacherByTno(String tno){
		//准备sql查询语句
		String checkSql = "select * from teacher where tno=?";
		//准备参数
		String[] checkParam = { tno };
		//调用数据库工具类的查询方法，执行查询语句
		rs = db.executeQuery(checkSql, checkParam);
		Teacher result = null;
		try {
			if(rs.next()){
				result = new Teacher();
				result.setName(rs.getString("name"));
				result.setTno(rs.getString("tno"));
				result.setDepartment(rs.getString("dno"));
				result.setSex(rs.getString("sex"));
				result.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			result = null;
		}
		return result;
	}

	/**
	 * 通过学工号获取老师信息
	 * @param name 老师名字
	 * @return 老师对象
	 */
	public Teacher getTeacherByName(String name){
		//准备sql查询语句
		String checkSql = "select * from teacher where name=?";
		//准备参数
		String[] checkParam = { name };
		//调用数据库工具类的查询方法，执行查询语句
		rs = db.executeQuery(checkSql, checkParam);
		Teacher result = null;
		try {
			if(rs.next()){
				result = new Teacher();
				result.setName(rs.getString("name"));
				result.setTno(rs.getString("tno"));
				result.setDepartment(rs.getString("dno"));
				result.setSex(rs.getString("sex"));
				result.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			result = null;
		}
		return result;
	}

	/**
	 * 获取全部老师信息
	 * @return 老师列表
	 */
	public List<Teacher> getAllTeachers(){
		String sql = "select * from teacher";
		ResultSet rs = db.executeQuery(sql);
		List<Teacher> teacherList = new ArrayList<>();
		try {
			while (rs.next()){
				Teacher teacher = new Teacher();
				teacher.setTno(rs.getString("tno"));
				teacher.setName(rs.getString("name"));
				teacher.setSex(rs.getString("sex"));
				teacher.setDepartment(rs.getString("dno"));
				teacherList.add(teacher);
			}
		} catch (SQLException throwables) {

		}
		return teacherList;
	}

}
